package tx.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tx.dao.PhoneDao;

@Service
public class PhoneService {

  @Autowired
  private PhoneDao phoneDao;

  @Transactional
 // @Transactional(noRollbackFor = BadSqlGrammarException.class)
  //根据手机的id购买手机
  public void purchase(String userName, String id) {
    //1.根据id查询手机的价格
    int price = phoneDao.findPriceById(id);

    //2.根据用户名和价格更新用户余额
    phoneDao.updateBalance(userName, price);

    //3.根据id更新手机的库存
    phoneDao.updateStock(id);
  }

  @Transactional(propagation= Propagation.REQUIRES_NEW)
  public void alterPrice(String id,int amount) {
    phoneDao.updatePrice(id,amount);
  }
}
